package com.xxx.service.seviceImpl;

import com.xxx.eneity.User;
import com.xxx.mapper.UserMapper;
import com.xxx.service.UserService;
import com.xxx.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public User getUser(String userName, String password) {

        int userCount = userMapper.getUserCount(userName);
        if (userCount > 0) {
            return userMapper.getUser(userName, password);
        } else {
            return null;
        }
    }

    @Override
    public boolean isCodeRegistered(Integer code) {
        int result = userMapper.isCodeRegistered(code);
        if (result > 0) {
            return true;
        }
        return false;
    }

    @Override
    public Integer getRole(Integer code) {
        Integer result = userMapper.getRole(code);
        return result;
    }

    @Override
    public boolean isCodeExist(Integer code) {
        int count = userMapper.getCountByCode(code);
        if (count > 0) {
            return true;
        }
        return false;
    }

    @Override
    public List<UserVo> getUserList() {
        return userMapper.getUserList();
    }

    @Override
    public User getUserInfo(Integer userId) {
        return userMapper.getUserById(userId);
    }

    @Override
    public int editUser(User user) {
        return userMapper.editUser(user);
    }

    @Override
    public int editPassword(Integer userId, String newpwd) {
        return userMapper.editPassword(userId, newpwd);
    }

    @Override
    public int getCount(Integer userId, String oldpwd) {
        return userMapper.getCount(userId, oldpwd);
    }

    @Override
    public boolean isUserExist(String userName) {
        int userCount = userMapper.getUserCount(userName);
        if (userCount > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public int register(User user) {
        int result = userMapper.register(user);
        // 获取用户id
        Integer userId = userMapper.getUserId(user);
        user.setId(userId);
        userMapper.updateCode(user);
        return result;
    }
}
